/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.getkeepsafe.relinker.sample.slice;

import com.getkeepsafe.relinker.ReLinker;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer.LayoutConfig;
import ohos.agp.components.PositionLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * Function description
 * class MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final String TAG_LOG = "[Relinker Sample] ";

    private static final String LIBRARY_NMAE = "hello";

    private static final String LIBRARY_VERSION = "1.0";

    private static final int DOMAIN_ID = 0xD000F00;

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, TAG_LOG);

    private static final int RED = 25;

    private static final int GREEN = 255;

    private static final int BLUE = 255;

    private static final int TEXT_SIZE = 75;

    private ReLinker.Logger logger = message -> HiLog.info(LABEL_LOG, message);

    private final Text text = new Text(this);

    private PositionLayout myLayout = new PositionLayout(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LayoutConfig config = new LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);
        ShapeElement element = new ShapeElement();
        element.setShape(ShapeElement.RECTANGLE);
        element.setRgbColor(new RgbColor(RED, GREEN, BLUE));
        myLayout.setBackground(element);
        loadNativeString();
        text.setTextColor(Color.BLACK);
        text.setTextSize(TEXT_SIZE);
        myLayout.addComponent(text);
        super.setUIContent(myLayout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void loadNativeString() {
        try {
            text.setText(stringFromjni());
        } catch (UnsatisfiedLinkError e) {
            HiLog.debug(LABEL_LOG, e.getLocalizedMessage());
            HiLog.debug(LABEL_LOG, "Loading library using Relinker lib...");
            ReLinker.log(logger).force().recursively().loadLibrary(this, LIBRARY_NMAE,
                    LIBRARY_VERSION, new ReLinker.LoadListener() {
                @Override
                public void success() {
                    text.setText(stringFromjni());
                }

                @Override
                public void failure(Throwable throwable) {
                    text.setText("Couldn't load native library...");
                    HiLog.debug(LABEL_LOG, throwable.getMessage());
                }
            });
        }
    }

    /**
     * Get String from JNI
     *
     * @return String
     */
    public native String stringFromjni();
}
